﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;
using System.Collections.Generic;

public partial class admin_UserList : System.Web.UI.Page
{
    private static DataSet ds = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Session["ShowMessage"] = "用户未登录，请重新登录!";
            Session["Backurl"] = "Login.aspx";
            Response.Redirect("~/Message.aspx");
        }
        if (!IsPostBack)
        {
            ds = UserManager.FindBlurUser(new User(), new UserInfo(), 2);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            if (Request.QueryString["sucess"] != null && Request.QueryString["sucess"] != "")
            {
                string sucess = Request.QueryString["sucess"];
                if (sucess == "1")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('操作成功！');</script>");
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('操作失败！');</script>");
                }
            }
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "updateState")
        {
            Response.Redirect("AddDimission.aspx?uid=" + e.CommandArgument.ToString());
        }
        else if(e.CommandName=="updateInfo")
        {
            Response.Redirect("UpdateUser.aspx?loginName="+e.CommandArgument.ToString());
        }
        else if(e.CommandName == "resetPassword")
        {
            TarreOA.TarreOAModels.User u = new User();
            u.NewPassword = "123456";
            u.LoginName = e.CommandArgument.ToString();
            if(UserManager.ChangePassword(u))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改成功！')", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改失败!请重试')", true);
            }
        }
        else if (e.CommandName == "delUser")
        {
            TarreOA.TarreOAModels.User u = new User();
            u.UserId = Convert.ToInt32(e.CommandArgument.ToString());
            if (UserManager.DelUser(u))
            {
                int npindex = GridView1.PageIndex;
                TarreOA.TarreOAModels.User user = new User();
                UserInfo userInfo = new UserInfo();
                ds = UserManager.FindBlurUser(user, userInfo,2);
                int pageindex = GridView1.PageIndex;
                GridView1.DataSource = ds;
                GridView1.PageIndex = pageindex;
                GridView1.DataBind();
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除成功！')", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除失败!请重试')", true);
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TarreOA.TarreOAModels.User user = new User();
        UserInfo userInfo = new UserInfo();
        if (txtLoginName.Text != null && txtLoginName.Text != "")
        {
            user.LoginName = txtLoginName.Text.Trim();
        }
        if (txtUserName.Text != null && txtUserName.Text != "")
        {
            user.UserName = txtUserName.Text.Trim();
        }
        if (txtJoinTime.Text != null && txtJoinTime.Text != "")
        {
            userInfo.JoinTime = Convert.ToDateTime(txtJoinTime.Text);
        }    
        if (ddlSection.SelectedValue != "0")
        {
            Section section = new Section();
            section.SectionId = Convert.ToInt32(ddlSection.SelectedValue);
            user.Section = section;
        }
        if (ddlAcademicCertificate.SelectedValue != "0")
        {
            userInfo.AcademicCertificate = Convert.ToInt32(ddlAcademicCertificate.SelectedValue);
        }
        ds = UserManager.FindBlurUser(user, userInfo, Convert.ToInt32(ddlState.SelectedValue));
        GridView1.DataSource = ds;
        ViewState["userstate"] = Convert.ToInt32(ddlState.SelectedValue);
        ViewState["userdata"] = user;
        ViewState["userinfodata"] = userInfo;
        GridView1.DataBind();
    }
    protected void imgbtnFind_Click(object sender, ImageClickEventArgs e)
    {
        TarreOA.TarreOAModels.User user = new User();
        UserInfo userInfo = new UserInfo();
        if (txtLoginName.Text != null && txtLoginName.Text != "")
        {
            user.LoginName = txtLoginName.Text.Trim();
        }
        if (txtUserName.Text != null && txtUserName.Text != "")
        {
            user.UserName = txtUserName.Text.Trim();
        }
        if (txtJoinTime.Text != null && txtJoinTime.Text != "")
        {
            userInfo.JoinTime = Convert.ToDateTime(txtJoinTime.Text);
        }
        if (ddlSection.SelectedValue != "0")
        {
            Section section = new Section();
            section.SectionId = Convert.ToInt32(ddlSection.SelectedValue);
            user.Section = section;
        }
        if (ddlAcademicCertificate.SelectedValue != "0")
        {
            userInfo.AcademicCertificate = Convert.ToInt32(ddlAcademicCertificate.SelectedValue);
        }
        ds = UserManager.FindBlurUser(user, userInfo, Convert.ToInt32(ddlState.SelectedValue));
        GridView1.DataSource = ds;
        ViewState["userstate"] = Convert.ToInt32(ddlState.SelectedValue);
        ViewState["userdata"] = user;
        ViewState["userinfodata"] = userInfo;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.DataSource = ds;
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
}
